package com.spsy.orders.listener;

import com.alibaba.fastjson2.JSON;
import com.spsy.orders.constant.MqConstant;
import com.spsy.orders.domain.Orders;
import com.spsy.orders.service.IOrdersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
import java.util.Date;

/**
 * 延迟队列消费者
 */
@Slf4j
@Component
public class DelayQueueConsumer {

    @Autowired
    private IOrdersService ordersService;


    @RabbitListener(queues = "lzp_delayed_queue_2206a7")
    private void receiveDelayQueue(Message message) {
        //String orderId = new String(message.getBody());

        try {
            String orderId = new String(message.getBody(), StandardCharsets.UTF_8);

            log.info("123订单号 orderId:{}", orderId);
            //通过msg=订单号id
            //修改数据库的数据
            Orders order = this.ordersService.selectOrdersByOrderId(Long.valueOf(orderId));

            log.info("订单号 orderId:{} order:{}", JSON.toJSONString(order));
            if (null == order) {
                return;
            }

            if (order.getOrderStatus().equals("1")) {
                order.setOrderStatus("4");
                int updateCount = this.ordersService.updateOrders(order);
                log.info("updateCount:{}", updateCount);
            } else {
                log.info("订单状态已经不是未支付了，order:{}", JSON.toJSONString(order));
            }

            log.info("=====>2206a 收到延迟消息----当前时间{}，收到延迟队列的消息{}", new Date(), orderId);
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}